/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Jan 8, 2012, 11:30:13 PM
 */
package GUI;

import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import Bussiness.ThanhToan;
import DatabaseAccess.DBHelper01;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Admin
 */
public class jpThanhToan extends javax.swing.JPanel {

    static String bot;
    private DefaultTableModel modelTable = new DefaultTableModel();

    /** Creates new form NewJPanel */
    public jpThanhToan() {
        initComponents();
        bot = "";
        this.display();
        this.teststatus();
    }

    public void reset() {
        txtMaNV.setText("");
        txtMaNCC.setText("");
        txtSoPC.setText("");
        txtNgayChi.setText("");
        txtNoiDung.setText("");
        txtXacNhan.setText("");
    }

    public void add() {
        ThanhToan b = new ThanhToan();
        b.setMaNV(txtMaNV.getText());
        b.setMaNCC(txtMaNCC.getText());
        b.setSoPhieuChi(txtSoPC.getText());
        b.setNgayChi(txtNgayChi.getText());
        b.setLyDo(txtNoiDung.getText());
        b.setXacNhan(txtXacNhan.getText());
        int t = 0;
        try {
            t = b.insertThanhToan(b);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (t == 1) {
            JOptionPane.showMessageDialog(this, "Add successful", "Infofrm", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Add erro!");
        }
        this.reset();
    }

    public void update() {
        ThanhToan c = new ThanhToan();
        c.setMaNV(txtMaNV.getText());
        c.setMaNCC(txtMaNCC.getText());
        c.setSoPhieuChi(txtSoPC.getText());
        c.setNgayChi(txtNgayChi.getText());
        c.setLyDo(txtNoiDung.getText());
        c.setXacNhan(txtXacNhan.getText());
        int t = 0;
        try {
            t = c.updateThanhToan(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (t == 1) {
            JOptionPane.showMessageDialog(this, "UPdate successful", "Infofrm", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Update erro!");
        }

    }

    public void loadTable(Vector v) {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã NV");
        v1.addElement("Mã NCC");
        v1.addElement("Số Phiếu Chi");
        v1.addElement("Ngày chi");
        v1.addElement("Nội dung");
        v1.addElement("Xác Nhận");
        modelTable.setColumnIdentifiers(v1);
        ThanhToan b = new ThanhToan();
        for (int i = 0; i < v.size(); i++) {
            b = (ThanhToan) v.get(i);
            Vector row = new Vector();
            row.addElement(b.getMaNV());
            row.addElement(b.getMaNCC());
            row.addElement(b.getSoPhieuChi());
            row.addElement(b.getNgayChi());
            row.addElement(b.getLyDo());
            row.addElement(b.getXacNhan());
            modelTable.addRow(row);
        }
        tblThanhToan.setModel(modelTable);
        int r = tblThanhToan.getSelectedRow();
        if (r < 0) {
            this.bot = "";
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
            return;
        } else {
            bot = tblThanhToan.getValueAt(r, 0).toString();
        }
    }

    public void teststatus() {
        if (FrmLogin.status == "nhanvien") {
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
        }
    }

    public void display() {
        ThanhToan b1 = new ThanhToan();
        Vector v1 = new Vector();
        try {
            v1 = b1.getAllThanhToan();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v1);
    }
    /*
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton5 = new javax.swing.JButton();
        txtNoiDung = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnDelete = new javax.swing.JButton();
        txtMaNCC = new javax.swing.JTextField();
        txtNgayChi = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtSoPC = new javax.swing.JTextField();
        btnEdit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblThanhToan = new javax.swing.JTable();
        txtMaNV = new javax.swing.JTextField();
        btnLoad = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        btnThem = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtXacNhan = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        jButton5.setText("In Hóa Đơn");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel2.setText("Mã NCC");

        btnDelete.setText("Xóa");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        jLabel4.setText("Ngày Chi");

        jLabel6.setText("Xác Nhận");

        jLabel1.setText("Mã NV");

        btnEdit.setText("Sửa");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        tblThanhToan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblThanhToan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblThanhToanMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblThanhToan);

        btnLoad.setText("Load");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        jLabel3.setText("Số Phiếu Chi");

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        jLabel5.setText("Nội dung");

        jLabel7.setText("Quản lý Thanh Toán ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(526, 526, 526)
                        .addComponent(jButton5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtMaNCC)
                                    .addComponent(txtMaNV, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtSoPC)
                                    .addComponent(txtNgayChi, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(17, 17, 17)
                                        .addComponent(txtNoiDung, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(txtXacNhan, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(45, 45, 45)
                                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(42, 42, 42)
                                .addComponent(btnLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtNoiDung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtSoPC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtNgayChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(txtXacNhan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtMaNCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLoad)
                    .addComponent(btnThem)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
// TODO add your handling code here:
    this.add();
    this.display();
}//GEN-LAST:event_btnThemActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
// TODO add your handling code here:
    if (!bot.equals("")) {
        int show = JOptionPane.showConfirmDialog(this, "Are You sure delete ?", "Inform", JOptionPane.YES_NO_OPTION);
        int t = 0, count = 0;
        ThanhToan b = new ThanhToan();
        if (show == JOptionPane.YES_OPTION) {
            int[] rArr = tblThanhToan.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    t = b.DelThanhToan(tblThanhToan.getValueAt(rArr[i], 0).toString());
                    if (t == 1) {
                        count++;
                    }
                }
                if (count > 0) {
                    JOptionPane.showMessageDialog(this, "Deleted " + count + " record ", "successful.", JOptionPane.INFORMATION_MESSAGE);
                    this.display();
                } else {
                    JOptionPane.showMessageDialog(this, "Error", "Inform", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException x) {
                System.out.println(x);
            }
        }
    }
    this.reset();
}//GEN-LAST:event_btnDeleteActionPerformed

private void tblThanhToanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblThanhToanMouseClicked
// TODO add your handling code here:
    int r = tblThanhToan.getSelectedRow();
    if (r < 0) {
        bot = "";
        return;
    } else {
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        bot = tblThanhToan.getValueAt(r, 0).toString();

        if (r != -1) {
            txtMaNV.setText(String.valueOf(tblThanhToan.getValueAt(r, 0)));
            txtMaNCC.setText(String.valueOf(tblThanhToan.getValueAt(r, 1)));
            txtSoPC.setText(String.valueOf(tblThanhToan.getValueAt(r, 2)));
            txtNgayChi.setText(String.valueOf(tblThanhToan.getValueAt(r, 3)));
            txtNoiDung.setText(String.valueOf(tblThanhToan.getValueAt(r, 4)));
            txtXacNhan.setText(String.valueOf(tblThanhToan.getValueAt(r, 5)));
        }
    }
}//GEN-LAST:event_tblThanhToanMouseClicked

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
// TODO add your handling code here:
    this.update();
    this.display();
}//GEN-LAST:event_btnEditActionPerformed

private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
// TODO add your handling code here:
    this.display();
}//GEN-LAST:event_btnLoadActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
        Connection con = new DBHelper01().DBHelper01();
        Map<String,Object> param = new HashMap<String,Object>();
        try{
        //"Documents\NetBeansProjects\DemoOnThi\src"
        String path = ".\\src\\BaoCaoThanhToan02.jrxml";
        JasperReport jasperReport = JasperCompileManager.compileReport(path);
        JasperPrint print = JasperFillManager.fillReport(jasperReport, param,con);
        //JasperPrint print = JasperFillManager.fillReport(path, param, con);
        JasperViewer.viewReport(print, false);
        }catch(Exception e){
            try {
                String path = ".\\src\\BaoCaoThanhToan02.jrxml";
                JasperReport jasperReport = JasperCompileManager.compileReport(path);
                JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
                //JasperPrint print = JasperFillManager.fillReport(path, param, con);
                JasperViewer.viewReport(print, false);
            } catch (JRException ex) {
               ex.printStackTrace();
            }
        }
}//GEN-LAST:event_jButton5ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblThanhToan;
    private javax.swing.JTextField txtMaNCC;
    private javax.swing.JTextField txtMaNV;
    private javax.swing.JTextField txtNgayChi;
    private javax.swing.JTextField txtNoiDung;
    private javax.swing.JTextField txtSoPC;
    private javax.swing.JTextField txtXacNhan;
    // End of variables declaration//GEN-END:variables
}
